package lichunUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class ManageTableFile_weka {
	
	static private String TableNameLine = "@relation";
	static private String AttributeInfoLine = "@attribute";
	static private String DataStartingLine = "@data";
	
	static public class TableInfo {
		public String name;
		public TableInfo(String n) {
			
		}
		public TableInfo(String n, ArrayList<String> attnlist) {
			
		}
		public TableInfo(String n, ArrayList<String> attnlist, ArrayList<String> atttlist) {
			
		}
		public void setattname(int id, String name) {
			
		}
		public void setatttype(int id, String type) {
			
		}
		public void addatt(String name) {
			
		}
		public void addatt(String name, String type) {
			
		}
	
		static public Map<String, Integer> attname2id (TableInfo info) {
			Map<String, Integer> res = new HashMap<String, Integer>();
			return res;
		}
	}
	
	static public TableInfo readtableinfo (String file, String enc) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), enc));
			TableInfo res = readtableinfo(br);
			br.close();
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		return null;
	}
	static public TableInfo readtableinfo (String file) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			TableInfo res = readtableinfo(br);
			br.close();
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		return null;
	}
	
	static public TableInfo readtableinfo (BufferedReader br) {
		String in;
		try {
			String name =null;
			while (true) {
				in = br.readLine();
				if (in.contains(TableNameLine)) {
					name = in.substring(in.indexOf(TableNameLine)+TableNameLine.length()).trim();
					break;
				}
			}
			ArrayList<String> attnamelist = new ArrayList<String>();
			ArrayList<String> atttypelist = new ArrayList<String>();
			while (true) {
				in = br.readLine();
				if (in.contains(AttributeInfoLine)) {
					String[] parts = in.split(" ", 3);
					String attname = parts[1];
					String atttype = parts[2];
					attnamelist.add(attname);
					atttypelist.add(atttype);
				}
				else if (in.equals(""))
					continue;
				else
					break;
			}
			while (true) {
				in = br.readLine();
				if (in.contains(DataStartingLine))
					break;
			}
			TableInfo res = new TableInfo(name, attnamelist, atttypelist);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		return null;
	}

	static public ArrayList<String> readlist (String attname) {
		ArrayList<String> res = new ArrayList<String>();
		return res;
	}
	static public ArrayList<String> readlist (int id) {
		ArrayList<String> res = new ArrayList<String>();
		return res;
	}
}
